// Generator : SpinalHDL v1.10.2a    git head : a348a60b7e8b6a455c72e1536ec3d74a2ea16935
// Component : sdio

`timescale 1ns/1ps

module sdio (
  output wire          io_sdio_clk,
  inout  wire [0:0]    io_sdio_cmd,
  inout  wire [3:0]    io_sdio_dat,
  input  wire          io_sdio_cmd_en,
  input  wire [5:0]    io_sdio_cmd_valu,
  input  wire [31:0]   io_sdio_cmd_argu,
  input  wire          io_sdio_cmd_need_rsp,
  input  wire          io_sdio_cmd_long_rsp,
  output wire [255:0]  io_sdio_rsp,
  input  wire          io_sdio_bus_4bit,
  input  wire [7:0]    io_sdio_clkdiv,
  input  wire          io_dat_next_en,
  input  wire [15:0]   io_block_size,
  input  wire          io_sdio_dat_en,
  input  wire          io_sdio_dat_write,
  output wire [31:0]   io_sdio_recv_dat,
  output wire          io_sdio_recv_dat_wr,
  output wire          io_recv_stream_valid,
  input  wire          io_recv_stream_ready,
  output wire [31:0]   io_recv_stream_payload,
  input  wire          io_send_stream_valid,
  output wire          io_send_stream_ready,
  input  wire [31:0]   io_send_stream_payload,
  input  wire [31:0]   io_timeout_tick,
  input  wire          io_timeout_reset,
  output wire          io_busy,
  output reg           io_cmd_send_done,
  output reg           io_crc_rsp_ok,
  output wire          io_crc_dat_ok,
  output reg           io_crc_rsp_fail,
  output wire          io_crc_dat_fail,
  output wire          io_timeout,
  output reg           io_timeout_rsp,
  output reg           io_timeout_dat,
  output wire          io_dat_end,
  input  wire          clk,
  input  wire          reset
);
  localparam SdioStatus_idle = 4'd0;
  localparam SdioStatus_cmd_init = 4'd1;
  localparam SdioStatus_cmd = 4'd2;
  localparam SdioStatus_rsp_wait = 4'd3;
  localparam SdioStatus_rsp = 4'd4;
  localparam SdioStatus_recv_dat_crc = 4'd5;
  localparam SdioStatus_send_dat_crc = 4'd6;
  localparam SdioStatus_send_dat_check = 4'd7;
  localparam SdioStatus_dat_wait = 4'd8;
  localparam SdioStatus_dat = 4'd9;
  localparam SdioStatus_dat_write_wait = 4'd10;
  localparam SdioStatus_dat_write = 4'd11;
  localparam SdioStatus_dat_wait_1bit = 4'd12;
  localparam SdioStatus_dat_1bit = 4'd13;
  localparam SdioStatus_dat_write_wait_1bit = 4'd14;
  localparam SdioStatus_dat_write_1bit = 4'd15;

  wire                sdio_crc16_4_io_sdio_line;
  wire                sdio_crc16_5_io_sdio_line;
  wire                sdio_crc16_6_io_sdio_line;
  wire                sdio_crc16_7_io_sdio_line;
  wire                sdio_crc8_1_io_calc_en;
  wire                sdio_crc8_1_io_crc_reset;
  wire       [15:0]   sdio_crc16_4_io_line_crc16;
  wire       [15:0]   sdio_crc16_5_io_line_crc16;
  wire       [15:0]   sdio_crc16_6_io_line_crc16;
  wire       [15:0]   sdio_crc16_7_io_line_crc16;
  wire       [7:0]    sdio_crc8_1_io_crc_out;
  wire       [3:0]    _zz_io_sdio_dat_3;
  wire       [0:0]    _zz_io_sdio_cmd_2;
  reg                 _zz_io_sdio_cmd;
  reg                 _zz_io_sdio_dat;
  reg        [7:0]    cmd_remaining;
  reg        [8:0]    _zz_when_sdio_l405;
  reg        [7:0]    _zz_when_sdio_l506;
  reg        [47:0]   _zz_io_sdio_cmd_1;
  reg                 when_sdio_l278;
  reg                 _zz_io_sdio_clk;
  reg        [255:0]  _zz_io_sdio_rsp;
  reg        [7:0]    _zz_when_sdio_l334;
  reg        [31:0]   _zz_io_sdio_dat_1;
  reg        [15:0]   _zz_when_sdio_l451;
  reg                 when_sdio_l284;
  reg        [31:0]   _zz_io_sdio_recv_dat;
  reg        [7:0]    recv_dat_len;
  reg        [3:0]    status;
  reg        [6:0]    _zz_when_sdio_l605;
  wire       [11:0]   rsp3;
  reg        [15:0]   line0_crc;
  wire                _zz_io_line_reset;
  reg                 _zz_io_calc_en;
  reg        [15:0]   _zz_io_sdio_dat_2;
  reg        [5:0]    _zz_when_sdio_l541;
  reg        [7:0]    _zz_when_sdio_l575;
  reg                 wait_recv_stream_ready;
  reg        [7:0]    debug_crc8;
  reg        [7:0]    rsp_crc8;
  wire                _zz_io_timeout_rsp;
  wire                _zz_io_timeout_dat;
  reg        [31:0]   _zz_io_timeout;
  wire                _zz_io_timeout_1;
  wire                when_sdio_l210;
  wire                when_sdio_l216;
  wire                when_sdio_l218;
  wire                when_sdio_l209;
  wire                _zz_io_cmd_send_done;
  wire                _zz_io_crc_dat_fail;
  wire                _zz_io_crc_dat_ok;
  wire                _zz_io_crc_rsp_ok;
  wire                _zz_io_crc_rsp_fail;
  reg                 when_sdio_l600;
  wire                _zz_when_sdio_l298;
  wire                _zz_when_sdio_l294;
  reg                 _zz_when_sdio_l294_1;
  wire                when_sdio_l294;
  reg                 _zz_when_sdio_l298_1;
  wire                when_sdio_l298;
  reg                 _zz_io_dat_end;
  wire                _zz_io_sdio_recv_dat_wr;
  wire                _zz_when_sdio_l334_1;
  wire                _zz_when_sdio_l334_2;
  wire                when_sdio_l334;
  wire                when_sdio_l335;
  wire                when_sdio_l337;
  reg                 io_sdio_cmd_en_regNext;
  wire                when_sdio_l350;
  wire       [3:0]    _zz_status;
  reg                 io_dat_next_en_regNext;
  wire                when_sdio_l354;
  reg                 _zz_when_sdio_l368;
  wire                when_sdio_l368;
  wire                when_sdio_l370;
  wire                when_sdio_l380;
  wire       [3:0]    _zz_status_1;
  reg                 _zz_when_sdio_l395;
  wire                when_sdio_l395;
  wire       [3:0]    _zz_status_2;
  reg                 _zz_when_sdio_l404;
  wire                when_sdio_l404;
  wire                when_sdio_l405;
  wire                when_sdio_l409;
  wire       [3:0]    _zz_status_3;
  wire       [3:0]    _zz_status_4;
  reg                 _zz_when_sdio_l430;
  wire                when_sdio_l430;
  wire       [3:0]    _zz_status_5;
  reg                 io_sdio_dat_en_regNext;
  wire                when_sdio_l434;
  reg                 io_sdio_dat_en_regNext_1;
  wire                when_sdio_l445;
  wire                when_sdio_l451;
  wire                when_sdio_l474;
  wire                when_sdio_l493;
  reg                 _zz_when_sdio_l469;
  wire                when_sdio_l469;
  reg                 io_sdio_clk_regNext;
  wire                when_sdio_l501;
  wire                when_sdio_l506;
  reg                 io_sdio_dat_en_regNext_2;
  wire                when_sdio_l520;
  wire                when_sdio_l524;
  reg                 _zz_when_sdio_l529;
  wire                when_sdio_l529;
  wire                when_sdio_l531;
  reg                 _zz_when_sdio_l540;
  wire                when_sdio_l540;
  wire                when_sdio_l541;
  wire                when_sdio_l566;
  reg                 _zz_when_sdio_l573;
  wire                when_sdio_l573;
  wire                when_sdio_l575;
  wire                when_sdio_l581;
  wire                _zz_when_sdio_l600;
  reg                 _zz_when_sdio_l600_1;
  wire                _zz_when_sdio_l600_2;
  reg                 _zz_when_sdio_l600_3;
  reg                 _zz_when_sdio_l604;
  wire                when_sdio_l604;
  wire                when_sdio_l605;
  wire                when_sdio_l625;
  wire                when_sdio_l626;
  reg                 io_sdio_clk_regNext_1;
  wire                when_sdio_l650;
  wire                when_sdio_l651;
  reg                 io_sdio_clk_regNext_2;
  reg                 _zz_io_recv_stream_valid;
  reg                 io_recv_stream_ready_regNext;
  reg                 _zz_io_sdio_recv_dat_wr_1;
  reg                 io_sdio_recv_dat_wr_regNext;
  `ifndef SYNTHESIS
  reg [151:0] status_string;
  reg [151:0] _zz_status_string;
  reg [151:0] _zz_status_1_string;
  reg [151:0] _zz_status_2_string;
  reg [151:0] _zz_status_3_string;
  reg [151:0] _zz_status_4_string;
  reg [151:0] _zz_status_5_string;
  `endif


  assign _zz_io_sdio_dat_3 = _zz_io_sdio_dat_1[31 : 28];
  assign _zz_io_sdio_cmd_2 = _zz_io_sdio_cmd_1[47 : 47];
  sdio_crc16 sdio_crc16_4 (
    .io_sdio_clk   (io_sdio_clk                     ), //i
    .io_sdio_line  (sdio_crc16_4_io_sdio_line       ), //i
    .io_calc_en    (_zz_io_calc_en                  ), //i
    .io_line_reset (_zz_io_line_reset               ), //i
    .io_line_crc16 (sdio_crc16_4_io_line_crc16[15:0]), //o
    .clk           (clk                             ), //i
    .reset         (reset                           )  //i
  );
  sdio_crc16 sdio_crc16_5 (
    .io_sdio_clk   (io_sdio_clk                     ), //i
    .io_sdio_line  (sdio_crc16_5_io_sdio_line       ), //i
    .io_calc_en    (_zz_io_calc_en                  ), //i
    .io_line_reset (_zz_io_line_reset               ), //i
    .io_line_crc16 (sdio_crc16_5_io_line_crc16[15:0]), //o
    .clk           (clk                             ), //i
    .reset         (reset                           )  //i
  );
  sdio_crc16 sdio_crc16_6 (
    .io_sdio_clk   (io_sdio_clk                     ), //i
    .io_sdio_line  (sdio_crc16_6_io_sdio_line       ), //i
    .io_calc_en    (_zz_io_calc_en                  ), //i
    .io_line_reset (_zz_io_line_reset               ), //i
    .io_line_crc16 (sdio_crc16_6_io_line_crc16[15:0]), //o
    .clk           (clk                             ), //i
    .reset         (reset                           )  //i
  );
  sdio_crc16 sdio_crc16_7 (
    .io_sdio_clk   (io_sdio_clk                     ), //i
    .io_sdio_line  (sdio_crc16_7_io_sdio_line       ), //i
    .io_calc_en    (_zz_io_calc_en                  ), //i
    .io_line_reset (_zz_io_line_reset               ), //i
    .io_line_crc16 (sdio_crc16_7_io_line_crc16[15:0]), //o
    .clk           (clk                             ), //i
    .reset         (reset                           )  //i
  );
  sdio_crc8 sdio_crc8_1 (
    .io_sdio_clk  (io_sdio_clk                ), //i
    .io_sdio_line (io_sdio_cmd                ), //i
    .io_calc_en   (sdio_crc8_1_io_calc_en     ), //i
    .io_crc_reset (sdio_crc8_1_io_crc_reset   ), //i
    .io_crc_out   (sdio_crc8_1_io_crc_out[7:0]), //o
    .io_crc_in    (sdio_crc8_1_io_crc_out[7:0]), //i
    .clk          (clk                        ), //i
    .reset        (reset                      )  //i
  );
  assign io_sdio_cmd = _zz_io_sdio_cmd ? _zz_io_sdio_cmd_2[0 : 0] : 1'bz;
  assign io_sdio_dat = _zz_io_sdio_dat ? _zz_io_sdio_dat_3[3 : 0] : 4'bzzzz;
  `ifndef SYNTHESIS
  always @(*) begin
    case(status)
      SdioStatus_idle : status_string = "idle               ";
      SdioStatus_cmd_init : status_string = "cmd_init           ";
      SdioStatus_cmd : status_string = "cmd                ";
      SdioStatus_rsp_wait : status_string = "rsp_wait           ";
      SdioStatus_rsp : status_string = "rsp                ";
      SdioStatus_recv_dat_crc : status_string = "recv_dat_crc       ";
      SdioStatus_send_dat_crc : status_string = "send_dat_crc       ";
      SdioStatus_send_dat_check : status_string = "send_dat_check     ";
      SdioStatus_dat_wait : status_string = "dat_wait           ";
      SdioStatus_dat : status_string = "dat                ";
      SdioStatus_dat_write_wait : status_string = "dat_write_wait     ";
      SdioStatus_dat_write : status_string = "dat_write          ";
      SdioStatus_dat_wait_1bit : status_string = "dat_wait_1bit      ";
      SdioStatus_dat_1bit : status_string = "dat_1bit           ";
      SdioStatus_dat_write_wait_1bit : status_string = "dat_write_wait_1bit";
      SdioStatus_dat_write_1bit : status_string = "dat_write_1bit     ";
      default : status_string = "???????????????????";
    endcase
  end
  always @(*) begin
    case(_zz_status)
      SdioStatus_idle : _zz_status_string = "idle               ";
      SdioStatus_cmd_init : _zz_status_string = "cmd_init           ";
      SdioStatus_cmd : _zz_status_string = "cmd                ";
      SdioStatus_rsp_wait : _zz_status_string = "rsp_wait           ";
      SdioStatus_rsp : _zz_status_string = "rsp                ";
      SdioStatus_recv_dat_crc : _zz_status_string = "recv_dat_crc       ";
      SdioStatus_send_dat_crc : _zz_status_string = "send_dat_crc       ";
      SdioStatus_send_dat_check : _zz_status_string = "send_dat_check     ";
      SdioStatus_dat_wait : _zz_status_string = "dat_wait           ";
      SdioStatus_dat : _zz_status_string = "dat                ";
      SdioStatus_dat_write_wait : _zz_status_string = "dat_write_wait     ";
      SdioStatus_dat_write : _zz_status_string = "dat_write          ";
      SdioStatus_dat_wait_1bit : _zz_status_string = "dat_wait_1bit      ";
      SdioStatus_dat_1bit : _zz_status_string = "dat_1bit           ";
      SdioStatus_dat_write_wait_1bit : _zz_status_string = "dat_write_wait_1bit";
      SdioStatus_dat_write_1bit : _zz_status_string = "dat_write_1bit     ";
      default : _zz_status_string = "???????????????????";
    endcase
  end
  always @(*) begin
    case(_zz_status_1)
      SdioStatus_idle : _zz_status_1_string = "idle               ";
      SdioStatus_cmd_init : _zz_status_1_string = "cmd_init           ";
      SdioStatus_cmd : _zz_status_1_string = "cmd                ";
      SdioStatus_rsp_wait : _zz_status_1_string = "rsp_wait           ";
      SdioStatus_rsp : _zz_status_1_string = "rsp                ";
      SdioStatus_recv_dat_crc : _zz_status_1_string = "recv_dat_crc       ";
      SdioStatus_send_dat_crc : _zz_status_1_string = "send_dat_crc       ";
      SdioStatus_send_dat_check : _zz_status_1_string = "send_dat_check     ";
      SdioStatus_dat_wait : _zz_status_1_string = "dat_wait           ";
      SdioStatus_dat : _zz_status_1_string = "dat                ";
      SdioStatus_dat_write_wait : _zz_status_1_string = "dat_write_wait     ";
      SdioStatus_dat_write : _zz_status_1_string = "dat_write          ";
      SdioStatus_dat_wait_1bit : _zz_status_1_string = "dat_wait_1bit      ";
      SdioStatus_dat_1bit : _zz_status_1_string = "dat_1bit           ";
      SdioStatus_dat_write_wait_1bit : _zz_status_1_string = "dat_write_wait_1bit";
      SdioStatus_dat_write_1bit : _zz_status_1_string = "dat_write_1bit     ";
      default : _zz_status_1_string = "???????????????????";
    endcase
  end
  always @(*) begin
    case(_zz_status_2)
      SdioStatus_idle : _zz_status_2_string = "idle               ";
      SdioStatus_cmd_init : _zz_status_2_string = "cmd_init           ";
      SdioStatus_cmd : _zz_status_2_string = "cmd                ";
      SdioStatus_rsp_wait : _zz_status_2_string = "rsp_wait           ";
      SdioStatus_rsp : _zz_status_2_string = "rsp                ";
      SdioStatus_recv_dat_crc : _zz_status_2_string = "recv_dat_crc       ";
      SdioStatus_send_dat_crc : _zz_status_2_string = "send_dat_crc       ";
      SdioStatus_send_dat_check : _zz_status_2_string = "send_dat_check     ";
      SdioStatus_dat_wait : _zz_status_2_string = "dat_wait           ";
      SdioStatus_dat : _zz_status_2_string = "dat                ";
      SdioStatus_dat_write_wait : _zz_status_2_string = "dat_write_wait     ";
      SdioStatus_dat_write : _zz_status_2_string = "dat_write          ";
      SdioStatus_dat_wait_1bit : _zz_status_2_string = "dat_wait_1bit      ";
      SdioStatus_dat_1bit : _zz_status_2_string = "dat_1bit           ";
      SdioStatus_dat_write_wait_1bit : _zz_status_2_string = "dat_write_wait_1bit";
      SdioStatus_dat_write_1bit : _zz_status_2_string = "dat_write_1bit     ";
      default : _zz_status_2_string = "???????????????????";
    endcase
  end
  always @(*) begin
    case(_zz_status_3)
      SdioStatus_idle : _zz_status_3_string = "idle               ";
      SdioStatus_cmd_init : _zz_status_3_string = "cmd_init           ";
      SdioStatus_cmd : _zz_status_3_string = "cmd                ";
      SdioStatus_rsp_wait : _zz_status_3_string = "rsp_wait           ";
      SdioStatus_rsp : _zz_status_3_string = "rsp                ";
      SdioStatus_recv_dat_crc : _zz_status_3_string = "recv_dat_crc       ";
      SdioStatus_send_dat_crc : _zz_status_3_string = "send_dat_crc       ";
      SdioStatus_send_dat_check : _zz_status_3_string = "send_dat_check     ";
      SdioStatus_dat_wait : _zz_status_3_string = "dat_wait           ";
      SdioStatus_dat : _zz_status_3_string = "dat                ";
      SdioStatus_dat_write_wait : _zz_status_3_string = "dat_write_wait     ";
      SdioStatus_dat_write : _zz_status_3_string = "dat_write          ";
      SdioStatus_dat_wait_1bit : _zz_status_3_string = "dat_wait_1bit      ";
      SdioStatus_dat_1bit : _zz_status_3_string = "dat_1bit           ";
      SdioStatus_dat_write_wait_1bit : _zz_status_3_string = "dat_write_wait_1bit";
      SdioStatus_dat_write_1bit : _zz_status_3_string = "dat_write_1bit     ";
      default : _zz_status_3_string = "???????????????????";
    endcase
  end
  always @(*) begin
    case(_zz_status_4)
      SdioStatus_idle : _zz_status_4_string = "idle               ";
      SdioStatus_cmd_init : _zz_status_4_string = "cmd_init           ";
      SdioStatus_cmd : _zz_status_4_string = "cmd                ";
      SdioStatus_rsp_wait : _zz_status_4_string = "rsp_wait           ";
      SdioStatus_rsp : _zz_status_4_string = "rsp                ";
      SdioStatus_recv_dat_crc : _zz_status_4_string = "recv_dat_crc       ";
      SdioStatus_send_dat_crc : _zz_status_4_string = "send_dat_crc       ";
      SdioStatus_send_dat_check : _zz_status_4_string = "send_dat_check     ";
      SdioStatus_dat_wait : _zz_status_4_string = "dat_wait           ";
      SdioStatus_dat : _zz_status_4_string = "dat                ";
      SdioStatus_dat_write_wait : _zz_status_4_string = "dat_write_wait     ";
      SdioStatus_dat_write : _zz_status_4_string = "dat_write          ";
      SdioStatus_dat_wait_1bit : _zz_status_4_string = "dat_wait_1bit      ";
      SdioStatus_dat_1bit : _zz_status_4_string = "dat_1bit           ";
      SdioStatus_dat_write_wait_1bit : _zz_status_4_string = "dat_write_wait_1bit";
      SdioStatus_dat_write_1bit : _zz_status_4_string = "dat_write_1bit     ";
      default : _zz_status_4_string = "???????????????????";
    endcase
  end
  always @(*) begin
    case(_zz_status_5)
      SdioStatus_idle : _zz_status_5_string = "idle               ";
      SdioStatus_cmd_init : _zz_status_5_string = "cmd_init           ";
      SdioStatus_cmd : _zz_status_5_string = "cmd                ";
      SdioStatus_rsp_wait : _zz_status_5_string = "rsp_wait           ";
      SdioStatus_rsp : _zz_status_5_string = "rsp                ";
      SdioStatus_recv_dat_crc : _zz_status_5_string = "recv_dat_crc       ";
      SdioStatus_send_dat_crc : _zz_status_5_string = "send_dat_crc       ";
      SdioStatus_send_dat_check : _zz_status_5_string = "send_dat_check     ";
      SdioStatus_dat_wait : _zz_status_5_string = "dat_wait           ";
      SdioStatus_dat : _zz_status_5_string = "dat                ";
      SdioStatus_dat_write_wait : _zz_status_5_string = "dat_write_wait     ";
      SdioStatus_dat_write : _zz_status_5_string = "dat_write          ";
      SdioStatus_dat_wait_1bit : _zz_status_5_string = "dat_wait_1bit      ";
      SdioStatus_dat_1bit : _zz_status_5_string = "dat_1bit           ";
      SdioStatus_dat_write_wait_1bit : _zz_status_5_string = "dat_write_wait_1bit";
      SdioStatus_dat_write_1bit : _zz_status_5_string = "dat_write_1bit     ";
      default : _zz_status_5_string = "???????????????????";
    endcase
  end
  `endif

  always @(*) begin
    _zz_io_sdio_cmd = 1'b0;
    if(when_sdio_l278) begin
      _zz_io_sdio_cmd = 1'b1;
    end
  end

  always @(*) begin
    _zz_io_sdio_dat = 1'b0;
    if(when_sdio_l284) begin
      _zz_io_sdio_dat = 1'b1;
    end
  end

  assign rsp3 = io_sdio_rsp[139 : 128];
  assign _zz_io_line_reset = 1'b0;
  assign _zz_io_timeout_rsp = 1'b0;
  always @(*) begin
    io_timeout_rsp = _zz_io_timeout_rsp;
    if(io_timeout_reset) begin
      io_timeout_rsp = 1'b0;
    end else begin
      if(!when_sdio_l209) begin
        if(when_sdio_l216) begin
          io_timeout_rsp = 1'b1;
        end
      end
    end
  end

  assign _zz_io_timeout_dat = 1'b0;
  always @(*) begin
    io_timeout_dat = _zz_io_timeout_dat;
    if(io_timeout_reset) begin
      io_timeout_dat = 1'b0;
    end else begin
      if(!when_sdio_l209) begin
        if(!when_sdio_l216) begin
          if(when_sdio_l218) begin
            io_timeout_dat = 1'b1;
          end
        end
      end
    end
  end

  assign _zz_io_timeout_1 = (io_timeout_tick < _zz_io_timeout);
  assign when_sdio_l210 = ((status == SdioStatus_rsp_wait) || (status == SdioStatus_dat_wait));
  assign io_timeout = _zz_io_timeout_1;
  assign when_sdio_l216 = (status == SdioStatus_rsp_wait);
  assign when_sdio_l218 = (status == SdioStatus_dat_wait);
  assign when_sdio_l209 = (! _zz_io_timeout_1);
  assign io_busy = (status != SdioStatus_idle);
  assign _zz_io_cmd_send_done = 1'b0;
  always @(*) begin
    io_cmd_send_done = _zz_io_cmd_send_done;
    case(status)
      SdioStatus_cmd_init : begin
        io_cmd_send_done = 1'b0;
      end
      SdioStatus_cmd : begin
        if(when_sdio_l368) begin
          if(!when_sdio_l380) begin
            io_cmd_send_done = (! io_sdio_cmd_need_rsp);
          end
        end
      end
      SdioStatus_rsp : begin
        if(when_sdio_l404) begin
          if(when_sdio_l409) begin
            io_cmd_send_done = 1'b1;
          end
        end
      end
      default : begin
      end
    endcase
  end

  assign _zz_io_crc_dat_fail = 1'b0;
  assign io_crc_dat_fail = _zz_io_crc_dat_fail;
  assign _zz_io_crc_dat_ok = 1'b0;
  assign io_crc_dat_ok = _zz_io_crc_dat_ok;
  assign _zz_io_crc_rsp_ok = 1'b0;
  always @(*) begin
    io_crc_rsp_ok = _zz_io_crc_rsp_ok;
    if(when_sdio_l294) begin
      io_crc_rsp_ok = 1'b0;
    end else begin
      if(when_sdio_l298) begin
        io_crc_rsp_ok = (_zz_io_sdio_rsp[7 : 0] == debug_crc8);
      end
    end
    case(status)
      SdioStatus_rsp_wait : begin
        io_crc_rsp_ok = 1'b0;
      end
      default : begin
      end
    endcase
  end

  assign _zz_io_crc_rsp_fail = 1'b0;
  always @(*) begin
    io_crc_rsp_fail = _zz_io_crc_rsp_fail;
    if(when_sdio_l294) begin
      io_crc_rsp_fail = 1'b0;
    end else begin
      if(when_sdio_l298) begin
        io_crc_rsp_fail = (_zz_io_sdio_rsp[7 : 0] != debug_crc8);
      end
    end
    case(status)
      SdioStatus_rsp_wait : begin
        io_crc_rsp_fail = 1'b0;
      end
      default : begin
      end
    endcase
  end

  assign io_sdio_recv_dat = _zz_io_sdio_recv_dat;
  assign io_sdio_clk = _zz_io_sdio_clk;
  assign io_sdio_rsp = _zz_io_sdio_rsp;
  assign sdio_crc16_4_io_sdio_line = io_sdio_dat[0];
  assign sdio_crc16_5_io_sdio_line = io_sdio_dat[1];
  assign sdio_crc16_6_io_sdio_line = io_sdio_dat[2];
  assign sdio_crc16_7_io_sdio_line = io_sdio_dat[3];
  assign sdio_crc8_1_io_crc_reset = ((status == SdioStatus_idle) || (status == SdioStatus_rsp_wait));
  assign sdio_crc8_1_io_calc_en = ((status == SdioStatus_cmd) || ((status == SdioStatus_rsp) && ($signed(_zz_when_sdio_l405) <= $signed(9'h080))));
  assign io_send_stream_ready = when_sdio_l600;
  assign _zz_when_sdio_l298 = (status == SdioStatus_rsp);
  assign _zz_when_sdio_l294 = (status == SdioStatus_rsp_wait);
  assign when_sdio_l294 = (_zz_when_sdio_l294 && (! _zz_when_sdio_l294_1));
  assign when_sdio_l298 = ((! _zz_when_sdio_l298) && _zz_when_sdio_l298_1);
  assign io_dat_end = _zz_io_dat_end;
  assign _zz_io_sdio_recv_dat_wr = ($signed(recv_dat_len) == $signed(8'h08));
  assign _zz_when_sdio_l334_1 = ((((((((($signed(8'h0) < $signed(cmd_remaining)) || ($signed(9'h0) < $signed(_zz_when_sdio_l405))) || ($signed(8'h0) < $signed(_zz_when_sdio_l506))) || ($signed(6'h0) < $signed(_zz_when_sdio_l541))) || ($signed(7'h0) < $signed(_zz_when_sdio_l605))) || (status == SdioStatus_rsp_wait)) || (status == SdioStatus_dat_wait)) || (status == SdioStatus_dat_write_wait)) || (status == SdioStatus_dat));
  assign _zz_when_sdio_l334_2 = ((status == SdioStatus_dat) && (_zz_io_sdio_recv_dat_wr && (! io_recv_stream_ready)));
  assign when_sdio_l334 = ((! _zz_io_sdio_clk) && (((((! _zz_io_sdio_clk) && ($signed(io_sdio_clkdiv) <= $signed(_zz_when_sdio_l334))) && _zz_when_sdio_l334_1) && (! _zz_when_sdio_l334_2)) || (status == SdioStatus_idle)));
  assign when_sdio_l335 = (_zz_io_sdio_clk && (((_zz_io_sdio_clk && ($signed(io_sdio_clkdiv) <= $signed(_zz_when_sdio_l334))) && _zz_when_sdio_l334_1) && (! _zz_when_sdio_l334_2)));
  assign when_sdio_l337 = ($signed(_zz_when_sdio_l334) < $signed(io_sdio_clkdiv));
  assign when_sdio_l350 = (io_sdio_cmd_en && (! io_sdio_cmd_en_regNext));
  assign _zz_status = (io_sdio_dat_write ? SdioStatus_dat_write_wait : SdioStatus_dat_wait);
  assign when_sdio_l354 = (io_dat_next_en && (! io_dat_next_en_regNext));
  assign when_sdio_l368 = (_zz_io_sdio_clk && (! _zz_when_sdio_l368));
  assign when_sdio_l370 = ($signed(cmd_remaining) == $signed(8'h09));
  assign when_sdio_l380 = ($signed(8'h01) < $signed(cmd_remaining));
  assign _zz_status_1 = (io_sdio_cmd_need_rsp ? SdioStatus_rsp_wait : SdioStatus_idle);
  assign when_sdio_l395 = (_zz_io_sdio_clk && (! _zz_when_sdio_l395));
  assign _zz_status_2 = ((io_sdio_cmd == 1'b0) ? SdioStatus_rsp : SdioStatus_rsp_wait);
  assign when_sdio_l404 = (_zz_io_sdio_clk && (! _zz_when_sdio_l404));
  assign when_sdio_l405 = ($signed(_zz_when_sdio_l405) == $signed(9'h009));
  assign when_sdio_l409 = ($signed(_zz_when_sdio_l405) <= $signed(9'h001));
  assign _zz_status_3 = (io_sdio_dat_write ? SdioStatus_dat_write_wait : SdioStatus_dat_wait);
  assign _zz_status_4 = (io_sdio_dat_en ? _zz_status_3 : SdioStatus_idle);
  assign when_sdio_l430 = (_zz_io_sdio_clk && (! _zz_when_sdio_l430));
  assign _zz_status_5 = (io_sdio_dat[0] ? SdioStatus_dat_wait : SdioStatus_dat);
  assign when_sdio_l434 = ((! io_sdio_dat_en) && io_sdio_dat_en_regNext);
  assign when_sdio_l445 = ((! io_sdio_dat_en) && io_sdio_dat_en_regNext_1);
  assign when_sdio_l451 = (($signed(_zz_when_sdio_l451) == $signed(16'h0001)) && ($signed(recv_dat_len) == $signed(8'h08)));
  assign when_sdio_l474 = ($signed(recv_dat_len) < $signed(8'h08));
  assign when_sdio_l493 = ((! io_recv_stream_ready) && ($signed(recv_dat_len) == $signed(8'h08)));
  assign when_sdio_l469 = (_zz_io_sdio_clk && (! _zz_when_sdio_l469));
  assign when_sdio_l501 = (io_sdio_clk && (! io_sdio_clk_regNext));
  assign when_sdio_l506 = ($signed(_zz_when_sdio_l506) <= $signed(8'h01));
  assign when_sdio_l520 = ((! io_sdio_dat_en) && io_sdio_dat_en_regNext_2);
  assign when_sdio_l524 = (($signed(_zz_when_sdio_l451) == $signed(16'h0001)) && ($signed(recv_dat_len) == $signed(8'h20)));
  assign when_sdio_l529 = (_zz_io_sdio_clk && (! _zz_when_sdio_l529));
  assign when_sdio_l531 = ($signed(recv_dat_len) < $signed(8'h20));
  assign when_sdio_l540 = (_zz_io_sdio_clk && (! _zz_when_sdio_l540));
  assign when_sdio_l541 = ($signed(6'h0) < $signed(_zz_when_sdio_l541));
  assign when_sdio_l566 = ($signed(_zz_when_sdio_l541) == $signed(6'h0));
  assign when_sdio_l573 = (_zz_io_sdio_clk && (! _zz_when_sdio_l573));
  assign when_sdio_l575 = _zz_when_sdio_l575[6];
  assign when_sdio_l581 = io_sdio_dat[0];
  assign _zz_when_sdio_l600 = ($signed(_zz_when_sdio_l605) == $signed(7'h08));
  assign _zz_when_sdio_l600_2 = ($signed(_zz_when_sdio_l605) == $signed(7'h20));
  assign when_sdio_l604 = (_zz_io_sdio_clk && (! _zz_when_sdio_l604));
  assign when_sdio_l605 = ($signed(7'h0) < $signed(_zz_when_sdio_l605));
  assign when_sdio_l625 = ($signed(_zz_when_sdio_l605) == $signed(7'h0));
  assign when_sdio_l626 = ($signed(_zz_when_sdio_l451) <= $signed(16'h0001));
  assign when_sdio_l650 = (io_sdio_clk && (! io_sdio_clk_regNext_1));
  assign when_sdio_l651 = ($signed(7'h0) < $signed(_zz_when_sdio_l605));
  assign io_recv_stream_valid = (((_zz_io_sdio_recv_dat_wr && (! _zz_io_recv_stream_valid)) && io_recv_stream_ready) || (wait_recv_stream_ready && (io_recv_stream_ready && (! io_recv_stream_ready_regNext))));
  assign io_recv_stream_payload = _zz_io_sdio_recv_dat;
  assign io_sdio_recv_dat_wr = (_zz_io_sdio_recv_dat_wr && (! _zz_io_sdio_recv_dat_wr_1));
  always @(posedge clk or posedge reset) begin
    if(reset) begin
      cmd_remaining <= 8'h0;
      _zz_when_sdio_l405 <= 9'h0;
      _zz_when_sdio_l506 <= 8'h0;
      _zz_io_sdio_cmd_1 <= 48'h0;
      when_sdio_l278 <= 1'b0;
      _zz_io_sdio_clk <= 1'b1;
      _zz_io_sdio_rsp <= 256'h0;
      _zz_when_sdio_l334 <= 8'h0;
      _zz_io_sdio_dat_1 <= 32'h0;
      _zz_when_sdio_l451 <= 16'h0;
      when_sdio_l284 <= 1'b0;
      _zz_io_sdio_recv_dat <= 32'h0;
      recv_dat_len <= 8'h0;
      status <= SdioStatus_idle;
      _zz_when_sdio_l605 <= 7'h0;
      line0_crc <= 16'h0;
      _zz_io_calc_en <= 1'b0;
      _zz_io_sdio_dat_2 <= 16'h0;
      _zz_when_sdio_l541 <= 6'h0;
      _zz_when_sdio_l575 <= 8'h0;
      wait_recv_stream_ready <= 1'b0;
      _zz_io_timeout <= 32'h0;
      when_sdio_l600 <= 1'b0;
      _zz_io_dat_end <= 1'b0;
    end else begin
      if(io_timeout_reset) begin
        _zz_io_timeout <= 32'h0;
      end else begin
        if(when_sdio_l209) begin
          if(when_sdio_l210) begin
            _zz_io_timeout <= (_zz_io_timeout + 32'h00000001);
          end
        end
      end
      if(when_sdio_l334) begin
        _zz_io_sdio_clk <= 1'b1;
      end
      if(when_sdio_l335) begin
        _zz_io_sdio_clk <= 1'b0;
      end
      if(when_sdio_l337) begin
        _zz_when_sdio_l334 <= ($signed(_zz_when_sdio_l334) + $signed(8'h01));
      end else begin
        _zz_when_sdio_l334 <= 8'h0;
      end
      case(status)
        SdioStatus_idle : begin
          when_sdio_l278 <= 1'b0;
          when_sdio_l284 <= 1'b0;
          _zz_io_sdio_dat_1 <= (~ 32'h0);
          if(when_sdio_l350) begin
            status <= SdioStatus_cmd_init;
          end else begin
            if(when_sdio_l354) begin
              status <= _zz_status;
            end
          end
        end
        SdioStatus_cmd_init : begin
          when_sdio_l278 <= 1'b1;
          _zz_io_sdio_cmd_1 <= {{{2'b01,io_sdio_cmd_valu},io_sdio_cmd_argu},8'hff};
          cmd_remaining <= 8'h30;
          status <= SdioStatus_cmd;
        end
        SdioStatus_cmd : begin
          if(when_sdio_l368) begin
            if(when_sdio_l370) begin
              _zz_io_sdio_cmd_1[47 : 41] <= sdio_crc8_1_io_crc_out[6 : 0];
            end else begin
              _zz_io_sdio_cmd_1 <= {_zz_io_sdio_cmd_1[46 : 0],1'b1};
            end
            if(when_sdio_l380) begin
              cmd_remaining <= ($signed(cmd_remaining) - $signed(8'h01));
            end else begin
              cmd_remaining <= 8'h0;
              status <= _zz_status_1;
              when_sdio_l278 <= 1'b0;
            end
          end
        end
        SdioStatus_rsp_wait : begin
          if(when_sdio_l395) begin
            status <= _zz_status_2;
            _zz_when_sdio_l405 <= (io_sdio_cmd_long_rsp ? 9'h088 : 9'h030);
          end
        end
        SdioStatus_rsp : begin
          if(when_sdio_l404) begin
            if(when_sdio_l409) begin
              status <= _zz_status_4;
            end
            _zz_when_sdio_l405 <= ($signed(_zz_when_sdio_l405) - $signed(9'h001));
            _zz_io_sdio_rsp <= {_zz_io_sdio_rsp[254 : 0],io_sdio_cmd};
          end
        end
        SdioStatus_dat_wait : begin
          when_sdio_l284 <= 1'b0;
          _zz_io_dat_end <= 1'b0;
          _zz_when_sdio_l451[13 : 0] <= io_block_size[15 : 2];
          _zz_when_sdio_l451[15 : 14] <= 2'b00;
          recv_dat_len <= 8'h0;
          if(when_sdio_l430) begin
            status <= _zz_status_5;
          end else begin
            if(when_sdio_l434) begin
              status <= SdioStatus_idle;
            end
          end
        end
        SdioStatus_dat : begin
          if(when_sdio_l445) begin
            status <= SdioStatus_idle;
          end else begin
            if(when_sdio_l451) begin
              status <= SdioStatus_recv_dat_crc;
              _zz_when_sdio_l506 <= 8'h10;
            end
          end
          if(wait_recv_stream_ready) begin
            if(io_recv_stream_ready) begin
              recv_dat_len <= 8'h0;
              wait_recv_stream_ready <= 1'b0;
              _zz_when_sdio_l451 <= ($signed(_zz_when_sdio_l451) - $signed(16'h0001));
            end
          end else begin
            if(when_sdio_l469) begin
              _zz_io_sdio_recv_dat <= {_zz_io_sdio_recv_dat[27 : 0],io_sdio_dat};
              if(when_sdio_l474) begin
                wait_recv_stream_ready <= 1'b0;
                recv_dat_len <= ($signed(recv_dat_len) + $signed(8'h01));
              end else begin
                if(io_recv_stream_ready) begin
                  recv_dat_len <= 8'h01;
                  _zz_when_sdio_l451 <= ($signed(_zz_when_sdio_l451) - $signed(16'h0001));
                end
              end
            end else begin
              if(when_sdio_l493) begin
                wait_recv_stream_ready <= 1'b1;
              end
            end
          end
        end
        SdioStatus_recv_dat_crc : begin
          if(when_sdio_l501) begin
            _zz_when_sdio_l506 <= ($signed(_zz_when_sdio_l506) - $signed(8'h01));
            if(when_sdio_l506) begin
              status <= SdioStatus_idle;
              _zz_io_dat_end <= 1'b1;
            end
            line0_crc <= {line0_crc[14 : 0],io_sdio_dat[0]};
          end
        end
        SdioStatus_dat_1bit : begin
          if(when_sdio_l520) begin
            status <= SdioStatus_idle;
          end else begin
            if(when_sdio_l524) begin
              status <= SdioStatus_recv_dat_crc;
              _zz_when_sdio_l506 <= 8'h10;
            end
          end
          if(when_sdio_l529) begin
            _zz_io_sdio_recv_dat <= {_zz_io_sdio_recv_dat[30 : 0],io_sdio_dat[0]};
            if(when_sdio_l531) begin
              recv_dat_len <= ($signed(recv_dat_len) + $signed(8'h01));
            end else begin
              recv_dat_len <= 8'h01;
            end
          end
        end
        SdioStatus_send_dat_crc : begin
          if(when_sdio_l540) begin
            if(when_sdio_l541) begin
              _zz_when_sdio_l541 <= ($signed(_zz_when_sdio_l541) - $signed(6'h01));
              if(io_sdio_bus_4bit) begin
                _zz_io_sdio_dat_2 <= {_zz_io_sdio_dat_2[14 : 0],1'b1};
                _zz_io_sdio_dat_1 <= {{{{_zz_io_sdio_dat_2[0],_zz_io_sdio_dat_2[1]},_zz_io_sdio_dat_2[2]},_zz_io_sdio_dat_2[3]},28'h0};
              end else begin
                _zz_io_sdio_dat_1 <= {_zz_io_sdio_dat_1[30 : 0],1'b1};
              end
            end else begin
              _zz_when_sdio_l575 <= 8'hff;
              status <= SdioStatus_send_dat_check;
            end
          end else begin
            if(when_sdio_l566) begin
              status <= SdioStatus_send_dat_check;
            end
          end
        end
        SdioStatus_send_dat_check : begin
          if(when_sdio_l573) begin
            if(when_sdio_l575) begin
              _zz_when_sdio_l575 <= {_zz_when_sdio_l575[6 : 0],io_sdio_dat[0]};
            end else begin
              if(when_sdio_l581) begin
                status <= SdioStatus_idle;
              end
            end
          end
        end
        SdioStatus_dat_write : begin
          if(io_sdio_bus_4bit) begin
            when_sdio_l600 <= (_zz_when_sdio_l600 && (! _zz_when_sdio_l600_1));
          end else begin
            when_sdio_l600 <= (_zz_when_sdio_l600_2 && (! _zz_when_sdio_l600_3));
          end
          if(when_sdio_l600) begin
            _zz_when_sdio_l451 <= ($signed(_zz_when_sdio_l451) - $signed(16'h0001));
          end
          if(when_sdio_l604) begin
            if(when_sdio_l605) begin
              _zz_when_sdio_l605 <= ($signed(_zz_when_sdio_l605) - $signed(7'h01));
              if(io_sdio_bus_4bit) begin
                _zz_io_sdio_dat_1 <= {_zz_io_sdio_dat_1[27 : 0],4'b1111};
              end else begin
                _zz_io_sdio_dat_1 <= {_zz_io_sdio_dat_1[30 : 0],1'b1};
              end
            end else begin
              if(io_send_stream_valid) begin
                _zz_when_sdio_l605 <= (io_sdio_bus_4bit ? 7'h08 : 7'h20);
                _zz_io_sdio_dat_1 <= io_send_stream_payload;
              end else begin
                status <= SdioStatus_dat_write_wait;
              end
            end
          end else begin
            if(when_sdio_l625) begin
              if(when_sdio_l626) begin
                status <= SdioStatus_send_dat_crc;
                _zz_when_sdio_l541 <= 6'h11;
                _zz_io_sdio_dat_2 <= sdio_crc16_4_io_line_crc16;
              end else begin
                if(io_send_stream_valid) begin
                  _zz_when_sdio_l605 <= (io_sdio_bus_4bit ? 7'h08 : 7'h20);
                  _zz_io_sdio_dat_1 <= io_send_stream_payload;
                end
              end
            end
          end
        end
        SdioStatus_dat_write_wait : begin
          when_sdio_l284 <= 1'b1;
          _zz_when_sdio_l451[13 : 0] <= io_block_size[15 : 2];
          _zz_when_sdio_l451[15 : 14] <= 2'b00;
          if(when_sdio_l650) begin
            if(when_sdio_l651) begin
              status <= SdioStatus_dat_write;
              _zz_io_sdio_dat_1 <= io_send_stream_payload;
            end else begin
              if(io_send_stream_valid) begin
                _zz_io_calc_en <= 1'b1;
                _zz_when_sdio_l605 <= (io_sdio_bus_4bit ? 7'h08 : 7'h20);
                _zz_io_sdio_dat_1 <= 32'h0;
              end else begin
                _zz_io_sdio_dat_1 <= (~ 32'h0);
              end
            end
          end
        end
        default : begin
        end
      endcase
    end
  end

  always @(posedge clk) begin
    _zz_when_sdio_l294_1 <= _zz_when_sdio_l294;
    _zz_when_sdio_l298_1 <= _zz_when_sdio_l298;
    case(status)
      SdioStatus_cmd : begin
        if(when_sdio_l368) begin
          if(when_sdio_l370) begin
            debug_crc8 <= {sdio_crc8_1_io_crc_out[6 : 0],1'b1};
          end
        end
      end
      SdioStatus_rsp : begin
        if(when_sdio_l404) begin
          if(when_sdio_l405) begin
            rsp_crc8 <= {sdio_crc8_1_io_crc_out[6 : 0],1'b1};
          end
        end
      end
      default : begin
      end
    endcase
    _zz_io_recv_stream_valid <= _zz_io_sdio_recv_dat_wr;
    io_recv_stream_ready_regNext <= io_recv_stream_ready;
    _zz_io_sdio_recv_dat_wr_1 <= _zz_io_sdio_recv_dat_wr;
    io_sdio_recv_dat_wr_regNext <= io_sdio_recv_dat_wr;
  end

  always @(posedge clk) begin
    io_sdio_cmd_en_regNext <= io_sdio_cmd_en;
    io_dat_next_en_regNext <= io_dat_next_en;
  end

  always @(posedge clk) begin
    _zz_when_sdio_l368 <= _zz_io_sdio_clk;
  end

  always @(posedge clk) begin
    _zz_when_sdio_l395 <= _zz_io_sdio_clk;
  end

  always @(posedge clk) begin
    _zz_when_sdio_l404 <= _zz_io_sdio_clk;
  end

  always @(posedge clk) begin
    _zz_when_sdio_l430 <= _zz_io_sdio_clk;
    io_sdio_dat_en_regNext <= io_sdio_dat_en;
  end

  always @(posedge clk) begin
    io_sdio_dat_en_regNext_1 <= io_sdio_dat_en;
    _zz_when_sdio_l469 <= _zz_io_sdio_clk;
  end

  always @(posedge clk) begin
    io_sdio_clk_regNext <= io_sdio_clk;
  end

  always @(posedge clk) begin
    io_sdio_dat_en_regNext_2 <= io_sdio_dat_en;
    _zz_when_sdio_l529 <= _zz_io_sdio_clk;
  end

  always @(posedge clk) begin
    _zz_when_sdio_l540 <= _zz_io_sdio_clk;
  end

  always @(posedge clk) begin
    _zz_when_sdio_l573 <= _zz_io_sdio_clk;
  end

  always @(posedge clk) begin
    _zz_when_sdio_l600_1 <= _zz_when_sdio_l600;
  end

  always @(posedge clk) begin
    _zz_when_sdio_l600_3 <= _zz_when_sdio_l600_2;
  end

  always @(posedge clk) begin
    _zz_when_sdio_l604 <= _zz_io_sdio_clk;
  end

  always @(posedge clk) begin
    io_sdio_clk_regNext_1 <= io_sdio_clk;
    io_sdio_clk_regNext_2 <= io_sdio_clk;
  end


endmodule

module sdio_crc8 (
  input  wire          io_sdio_clk,
  input  wire [0:0]    io_sdio_line,
  input  wire          io_calc_en,
  input  wire          io_crc_reset,
  output wire [7:0]    io_crc_out,
  input  wire [7:0]    io_crc_in,
  input  wire          clk,
  input  wire          reset
);

  reg        [7:0]    crc;
  wire       [7:0]    polynomial;
  wire       [8:0]    crc_shift;
  wire       [7:0]    crc_calc;
  wire                crc_calc_signal;
  reg                 io_sdio_clk_regNext;
  wire                when_sdio_l74;

  assign polynomial = 8'h09;
  assign crc_shift = {io_crc_in,1'b0};
  assign crc_calc = crc_shift[7 : 0];
  assign crc_calc_signal = (crc_calc[7] ^ io_sdio_line[0]);
  assign io_crc_out = crc;
  assign when_sdio_l74 = (((! io_sdio_clk) && io_sdio_clk_regNext) && io_calc_en);
  always @(posedge clk or posedge reset) begin
    if(reset) begin
      crc <= 8'h0;
    end else begin
      if(io_crc_reset) begin
        crc <= 8'h0;
      end else begin
        if(when_sdio_l74) begin
          crc <= (crc_calc ^ (crc_calc_signal ? polynomial : 8'h0));
        end
      end
    end
  end

  always @(posedge clk) begin
    io_sdio_clk_regNext <= io_sdio_clk;
  end


endmodule

//sdio_crc16_3 replaced by sdio_crc16

//sdio_crc16_2 replaced by sdio_crc16

//sdio_crc16_1 replaced by sdio_crc16

module sdio_crc16 (
  input  wire          io_sdio_clk,
  input  wire          io_sdio_line,
  input  wire          io_calc_en,
  input  wire          io_line_reset,
  output wire [15:0]   io_line_crc16,
  input  wire          clk,
  input  wire          reset
);

  reg        [15:0]   crc;
  wire       [15:0]   polynomial;
  wire       [16:0]   crc_shift;
  wire       [15:0]   crc_calc;
  reg                 io_sdio_clk_regNext;
  wire                when_sdio_l48;

  assign polynomial = 16'h1021;
  assign crc_shift = {crc,1'b0};
  assign crc_calc = (crc_shift[15 : 0] ^ (crc[15] ? polynomial : 16'h0));
  assign io_line_crc16 = crc;
  assign when_sdio_l48 = ((io_sdio_clk && (! io_sdio_clk_regNext)) && io_calc_en);
  always @(posedge clk or posedge reset) begin
    if(reset) begin
      crc <= 16'h0;
    end else begin
      if(io_line_reset) begin
        crc <= 16'h0;
      end else begin
        if(when_sdio_l48) begin
          crc <= (crc_calc[15 : 0] ^ (io_sdio_line ? polynomial : 16'h0));
        end
      end
    end
  end

  always @(posedge clk) begin
    io_sdio_clk_regNext <= io_sdio_clk;
  end


endmodule
